package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {


    @Autowired
    private MemberDao memberDao;


    //查询所有
    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    //根据id查询
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询会员信息
        Page<Member> page = memberDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增
    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    //修改
    @Override
    public void edit(Member member) {
        memberDao.update(member);
    }

    @Override
    public void update(Integer id, Integer status) {
        memberDao.updateStatus(id, status);
    }

    //        根据时间查询  累积计数
    @Override
    public List<Integer> findMemberByMonths(List<String> months) {
        List<Integer> dateCount = new ArrayList<>();
        for (String month : months) {
            String date = month + "-31";
            int count = this.memberDao.findMemberCountByDateMonths(date);
            dateCount.add(count);
        }

        return dateCount;

    }


}
